<template>
    <!-- 富文本文档
    示例：https://richtexteditor.com/demos/fileupload.aspx
    api：https://richtexteditor.com/docs/initialization.aspx -->
    <div>
        <div ref="main" class="main" style="min-height:400px;max-height:600px"></div>
    </div>
</template>

<script>
import uploadFile from '../api/uploadFile';

export default {
    data() {
        return {
            RTE: void 0
        }
    },
    mounted() {
        let config = {
            editorResizeMode: "none",
            file_upload_handler: file => {
                this.$message('正在上传中，请耐心等待...')
                let formData = new FormData()
                formData.append('file', file)
                uploadFile.uploadFile(formData).then(res => {
                    switch (res.data.match(/[^.]+$/)[0]) {
                        case "doc":
                        case "docx":
                        case "xls":
                        case "xlsx":
                        case "pdf":
                            this.RTE?.insertHTML(
                                `<a target="_blank" href="#/filePreview/filePreview?fileUrl=${res.data}&id=${this.$store.getters.token}">${file.name}</a><br/>`
                            )
                            this.$message(res.message)
                            break;
                        default:
                            this.$message(res.message)
                    }
                })
            }
        }
        this.RTE = new RichTextEditor(this.$refs.main, config)
    },
    methods: {
        getHTMLText() {
            return this.RTE?.getHTMLCode()
        }
    }
}
</script>

<style scoped></style>